/* 
 * File:   ThreadAble.h
 * Author: bastien
 *
 * Created on 12 octobre 2010, 11:57
 */

#ifndef THREADABLE_H
#define	THREADABLE_H

#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <iostream> //FIXME : to replace with custom log system
#include <string>

// #ifdef USEOF_THREADPOOL
#include "ThreadPool.h"

class ThreadPool;
// # endif

typedef void* (*Thread_Entry)(void *);

class ThreadAble {
public:
    ThreadAble();
    virtual ~ThreadAble();

    void run();
    void cancel();
    int join();
    void kill(int);
    void detach();
    bool isRunning();
    virtual void* exec();

protected:

    void attrSetDetached();
    void attrSetJoinable();

    void setAsynchronousCancel();
    void setDeferedCancel();
    void enableCancel();
    void disableCancel();

    void yield();
private:
    ThreadAble(const ThreadAble&);

    pthread_t threadid;
    pthread_attr_t threadattr;
    
    static void* launch_fct(void* obj){
        return reinterpret_cast<ThreadAble*>(obj)->exec();
    }

    int retCode;

    int errorHandle(int err);

};

#endif	/* THREADABLE_H */

